package com.webtest.demo;

import java.util.List;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import bsh.This;
import io.appium.java_client.AppiumBy;
import io.appium.java_client.android.AndroidDriver;

public class CrossearthPage {

	AndroidDriver  driver;
	BaseAction action;
	
	public CrossearthPage(AndroidDriver driver) {
		this.driver = driver;
		this.action= new BaseAction(driver);
	}
	
	
	//一、忘记密码页面
	//1.忘记密码页面，点击返回登录页面
	public WebElement forgetpagebackLogin() {
		return driver.findElement(AppiumBy
				.id("com.software.crossearth:id/backButton"));
	}
	//2.忘记密码页面，输入邮箱
	public WebElement forgetpageEmail() {
		return driver.findElement(AppiumBy
				.xpath("//android.widget.EditText[@text='邮箱验证']"));
	}
	public WebElement checkforgetpageEmail() {
		return driver.findElement(AppiumBy
				.id("com.software.crossearth:id/emailEditText"));
	}	
	//3.忘记密码页面，点击发送
	public WebElement forgetpageSend() {
		return driver.findElement(AppiumBy
				.id("com.software.crossearth:id/emailButton"));
	}
	//4.忘记密码页面，输入验证码
	public WebElement forgetpageCaptcha() {
		return driver.findElement(AppiumBy
				.xpath("//android.widget.EditText[@text='验证码']"));
	}
	public WebElement checkforgetpageCaptcha() {
		return driver.findElement(AppiumBy
				.id("com.software.crossearth:id/validateCodeEditText"));			
	}
	//5.输入新密码
	public WebElement newPassword() {
		return driver.findElement(AppiumBy
				.xpath("//android.widget.EditText[@text='新密码']"));
	}
	public WebElement checknewPassword() {
		return driver.findElement(AppiumBy
				.id("com.software.crossearth:id/NewPasswordEditText"));			
	}
	//6.输入确认新密码
	public WebElement confirmnewPassword() {
		return driver.findElement(AppiumBy
				.xpath("//android.widget.EditText[@text='确认新密码']"));
	}
	public WebElement checkconfirmnewPassword() {
		return driver.findElement(AppiumBy
				.id("com.software.crossearth:id/verifyNewPasswordEditText"));			
	}
	//7.点击修改
	public WebElement revise() {
		return driver.findElement(AppiumBy
				.id("com.software.crossearth:id/validateButton"));
	}

	//二、地图板块页面
	//1.获取地图朝代列表
	//每页第一个朝代对应index为0，最后一个对应index为7
	public WebElement mapDynasties(int i) {
		WebElement list=driver.findElement(AppiumBy
				.id("com.software.crossearth:id/history_recycler_view"));		
		List<WebElement> items=list.findElements(AppiumBy
				.className("android.widget.LinearLayout"));		
		//获取当页第i个历史事件
		WebElement historicalevents= items.get(i);
		return historicalevents;
	};
	//2.点击播放
	//3.点击暂停
	//4.拖拽调节声音
	//5.点击右上角地图图片
	public WebElement mapImage() {
		return driver.findElement(AppiumBy
				.id("com.software.crossearth:id/btn_drawer"));
	}
	//6.在地图板块，点击返回主页面
	public WebElement mapbackMainPage() {
		return driver.findElement(AppiumBy
				.id("com.software.crossearth:id/btn_drawer"));
	}
	//7.点击剧本名称进入剧本
	

	
	//三、其他
	public WebElement crossearth() {
		return driver.findElement(AppiumBy
				.xpath("//android.widget.TextView[@content-desc=\"CrossEarth\"]"));		
	}
	
	public void goback() throws InterruptedException{
		action.navigateBack();
	}
	
	public void gohome(){
		action.pressHomeKey();
	}
	
	public void enter(){
		action.pressEnterKey();
	}
	
	public void magnify(){
		action.zoomIn();
	}

	

	//一、忘记密码页面
	//26.不输入任何文本直接点击修改
	public void directlyclickRevise() throws InterruptedException {
		action.click(this.forgotPassword());
		action.click(this.revise());
		action.click(this.forgetpagebackLogin());
	}
	
	//27.输入错误的邮箱、合法新密码进行修改
	public void erroremailRevise() throws InterruptedException {
		action.click(this.forgotPassword());
		action.typeAndClear(this.forgetpageEmail(), "zhuying02@com");
		action.click(this.forgetpageSend());
		action.typeAndClear(this.forgetpageCaptcha(),"123456");
		action.typeAndClear(this.newPassword(), "zhuying02");
		action.type(this.confirmnewPassword(), "zhuying02");
		action.click(this.revise());
		action.click(this.forgetpagebackLogin());
	}
	
	//28.新密码为空、确认新密码不为空进行修改	
	public void newpasswordemptyRevise() throws InterruptedException {
		action.click(this.forgotPassword());
		action.typeAndClear(this.checkforgetpageEmail(), "zhuying02@02.com");
		action.click(this.forgetpageSend());
		action.typeAndClear(this.checkforgetpageCaptcha(),"98765");
		action.typeAndClear(this.checknewPassword(), "");
		action.type(this.checkconfirmnewPassword(), "zhuying02");
		action.click(this.revise());		
		action.getToastMsg("newpasswordemptyRevise");
		
		action.click(this.forgetpagebackLogin());
	}
	
	//29.邮箱及密码输入脚本函数进行修改
	public void scriptRevise() throws InterruptedException {
		action.click(this.forgotPassword());
		action.typeAndClear(this.checkforgetpageEmail(), "<script>alert('邮箱')</script>");
		action.click(this.forgetpageSend());
		action.typeAndClear(this.checkforgetpageCaptcha(),"654");
		action.typeAndClear(this.checknewPassword(), "<script>alert('新密码')</script>");
		action.type(this.checkconfirmnewPassword(), "<script>alert('确认新密码')</script>");
		action.click(this.revise());		
		action.getToastMsg("scriptRevise");
		
		action.click(this.forgetpagebackLogin());
	}
	
	//30.修改与原来相同的密码
	public void repeatpasswordRevise() throws InterruptedException {
		action.click(this.forgotPassword());
		action.typeAndClear(this.checkforgetpageEmail(), "zhuying22@22.com");
		action.click(this.forgetpageSend());
		action.typeAndClear(this.checkforgetpageCaptcha(),"321");
		action.typeAndClear(this.checknewPassword(), "zhuying22");
		action.type(this.checkconfirmnewPassword(), "zhuying22");
		action.click(this.revise());
		action.click(this.forgetpagebackLogin());
	}

	//二、地图板块页面
	//31.进入夏朝涂山之会剧本，只选择第一个按钮
	public void xiameetingtumountainMap() throws InterruptedException {
		action.click(this.crossearth());
		action.click(this.startCrossearth());
		action.click(this.quickLogin());
		action.click(this.map());
		Thread.sleep(15000);
		action.click(this.mapImage());
		//点击涂山之会
		action.click(522,451);
		Thread.sleep(1000);
		//点击进入剧本
		action.click(554,422);
		Thread.sleep(8000);
		//点击继续
		action.click(1232,880);
		Thread.sleep(300);
		//点击一介平民
		action.click(780,150);
		Thread.sleep(300);
		//点击继续
		for(int i = 0; i<9; ++i) {
			action.click(1232,880);
			Thread.sleep(300);
		}		
		//点击接近他们
		action.click(771,163);
		Thread.sleep(300);
		//点击继续
		for(int i = 0; i<19; ++i) {
			action.click(1232,880);
			Thread.sleep(300);
		}
		//点击返回，返回主页面
		action.click(this.mapbackMainPage());
	}
	
	//32.进入夏朝大禹治水剧本，插入查看朝代的步骤，接着下上按钮交替选择
	public void xiadayuwaterMap() throws InterruptedException {
		action.click(this.crossearth());
		action.click(this.startCrossearth());
		action.click(this.quickLogin());
		action.click(this.map());		
		Thread.sleep(15000);
		action.click(this.mapImage());
		//点击大禹治水
		action.click(854,368);
		Thread.sleep(1000);
		//点击进入剧本
		action.click(883,342);
		Thread.sleep(8000);
		
		action.click(this.mapImage());
		action.dragUp(this.mapDynasties(6));
		action.click(this.mapImage());
		
		//点击继续
		action.click(1232,880);
		Thread.sleep(300);
		//点击唐国公随身近侍
		action.click(810,291);
		Thread.sleep(300);
		//点击继续
		for(int i = 0; i<23; ++i) {
			action.click(1232,880);
			Thread.sleep(300);
		}		
		//点击假意阻拦
		action.click(771,151);
		Thread.sleep(300);
		//点击继续
		for(int i = 0; i<10; ++i) {
			action.click(1232,880);
			Thread.sleep(300);
		}
		//点击返回，返回主页面
		action.click(this.mapbackMainPage());
	}
	
	//33.在夏朝剧情中进入商朝，然后返回主页面
	public void xiaentershangMap() throws InterruptedException {
		action.click(this.crossearth());
		action.click(this.startCrossearth());
		action.click(this.quickLogin());
		action.click(this.map());	
		action.click(this.mapDynasties(1));
		Thread.sleep(15000);
		
		//点击返回，返回主页面
		action.click(this.mapbackMainPage());
	}

	//34.进入商朝武丁中兴剧本，只选择最下面的按钮
	public void shangwudingMap() throws InterruptedException {
		action.click(this.crossearth());
		action.click(this.startCrossearth());
		action.click(this.quickLogin());
		action.click(this.map());		
		action.click(this.mapDynasties(1));
		Thread.sleep(15000);
		//action.click(this.mapImage());
		
		//点击武丁中兴
		action.click(963,515);
		Thread.sleep(1000);
		//点击进入剧本
		action.click(995,483);
		Thread.sleep(8000);
		//点击继续
		action.click(1232,880);
		Thread.sleep(300);
		//点击唐国公随身近侍
		action.click(810,291);
		Thread.sleep(300);
		//点击继续
		for(int i = 0; i<23; ++i) {
			action.click(1232,880);
			Thread.sleep(300);
		}		
		//点击侧开身子
		action.click(781,291);
		Thread.sleep(300);
		//点击继续
		for(int i = 0; i<10; ++i) {
			action.click(1232,880);
			Thread.sleep(300);
		}
		//点击返回，返回主页面
		action.click(this.mapbackMainPage());
	}
	
	//35.进入商朝神道设教剧本前先点击牧野之战剧本，然后上下按钮交替选择
	public void shangbattlemakinoMap() throws InterruptedException {
		action.click(this.crossearth());
		action.click(this.startCrossearth());
		action.click(this.quickLogin());
		action.click(this.map());
		action.click(this.mapDynasties(1));
		Thread.sleep(15000);
		//action.click(this.mapImage());
		//点击牧野之战
		action.click(736,342);
		Thread.sleep(1000);
		//点击神道设教
		action.click(874,387);
		Thread.sleep(1000);
		//点击进入剧本
		action.click(909,355);
		Thread.sleep(8000);
		//点击继续
		action.click(1232,880);
		Thread.sleep(300);
		//点击一介平民
		action.click(780,150);
		Thread.sleep(300);
		//点击继续
		for(int i = 0; i<9; ++i) {
			action.click(1232,880);
			Thread.sleep(300);
		}		
		//点击远离他们
		action.click(774,298);
		Thread.sleep(300);
		//点击继续
		for(int i = 0; i<14; ++i) {
			action.click(1232,880);
			Thread.sleep(300);
		}
		//点击返回，返回主页面
		action.click(this.mapbackMainPage());
	}
		
	//36.在隋朝剧本中进入尉迟迥之乱前点击所有的剧本
	public void yuchirebellionMap() throws InterruptedException {
		action.click(this.crossearth());
		Thread.sleep(300);
		action.click(this.startCrossearth());
		Thread.sleep(300);
		action.click(this.quickLogin());
		Thread.sleep(300);
		action.click(this.map());
		Thread.sleep(300);
		action.swipe(154, 870, 154, 74);
		Thread.sleep(3000);
		//action.dragAndDrop(this.mapDynasties(6), this.mapDynasties(3));		
		//action.dragUp(this.mapDynasties(6));		
		//action.click(this.mapDynasties(6));
		
		action.click(240,634);
		Thread.sleep(18000);
		//action.click(this.mapImage());
		//点击大业盛世
		action.click(560,458);
		Thread.sleep(1000);
		//点击隋文帝改革
		action.click(662,413);
		Thread.sleep(1000);
		//点击晋阳起兵
		action.click(717,547);
		Thread.sleep(1000);
		//点击隋灭陈之战
		action.click(877,381);
		Thread.sleep(1000);
		//点击尉迟迥之乱
		action.click(739,448);
		Thread.sleep(1000);
		
		//点击进入剧本
		action.click(771,416);
		Thread.sleep(8000);
		//点击返回，返回主页面
		action.click(this.mapbackMainPage());
	}
	
	//37.在唐朝地图中不点击播放音乐，是否可以只调节音量的大小
	public void tangvolumeMap() throws InterruptedException {
		action.click(this.crossearth());
		Thread.sleep(300);
		action.click(this.startCrossearth());
		Thread.sleep(300);
		action.click(this.quickLogin());
		Thread.sleep(300);
		action.click(this.map());
		Thread.sleep(300);
		action.swipe(154, 870, 154, 74);
		Thread.sleep(3000);
		action.click(189,746);
		Thread.sleep(15000);
		//action.click(this.mapImage());
		//音量调小
		action.clickAndDrag(1050, 214, 1024, 214);
		//音量调大
		action.clickAndDrag(1024, 214, 1088, 214);
		//点击返回，返回主页面
		action.click(this.mapbackMainPage());
	}
	
	//38.在唐朝地图中先点击播放音乐，再点击暂停音乐
	public void tangplaypausemusicMap() throws InterruptedException {
		action.click(this.crossearth());
		Thread.sleep(300);
		action.click(this.startCrossearth());
		Thread.sleep(300);
		action.click(this.quickLogin());
		Thread.sleep(300);
		action.click(this.map());
		Thread.sleep(300);
		action.swipe(154, 870, 154, 74);
		Thread.sleep(3000);
		action.click(189,746);
		Thread.sleep(15000);
		//action.click(this.mapImage());
		//点击播放音乐
		action.click(1024,179);
		Thread.sleep(1000);
		//点击暂停音乐
		action.click(1072,182);
		Thread.sleep(8000);
		//点击返回，返回主页面
		action.click(this.mapbackMainPage());
	}
	
	//39.放大唐朝地图板块
	public void tangmagnifyMap() throws InterruptedException {
		action.click(this.crossearth());
		Thread.sleep(300);
		action.click(this.startCrossearth());
		Thread.sleep(300);
		action.click(this.quickLogin());
		Thread.sleep(300);
		action.click(this.map());
		Thread.sleep(300);
		action.swipe(154, 870, 154, 74);
		Thread.sleep(3000);
		action.click(189,746);
		Thread.sleep(15000);
		//action.click(this.mapImage());
	}
	
	//40.在唐朝地图中多次点击播放音乐
	public void tangmultipletimesmusicMap() throws InterruptedException {
		action.click(this.crossearth());
		Thread.sleep(300);
		action.click(this.startCrossearth());
		Thread.sleep(300);
		action.click(this.quickLogin());
		Thread.sleep(300);
		action.click(this.map());
		Thread.sleep(300);
		action.swipe(154, 870, 154, 74);
		Thread.sleep(3000);
		action.click(189,746);
		Thread.sleep(15000);
		//action.click(this.mapImage());
		
		//点击播放音乐
		for(int i=0;i<10;++i) {
			action.click(1024,179);
			Thread.sleep(300);
		}		
		//点击返回，返回主页面
		action.click(this.mapbackMainPage());
	}

}
